package com.xuhaijian.annotation;

import cn.hutool.json.JSONUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class LogInputOutputAspect {

    @Around("@annotation(LogInputOutput)") // 拦截带有 @LogInputOutput 注解的方法
    public Object logInputOutput(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取方法签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String methodName = signature.getMethod().getName();

        // 记录入参
        Object[] args = joinPoint.getArgs();
        System.out.println("Method " + methodName + "请求参数: " + Arrays.toString(args));

        // 执行方法
        Object result = joinPoint.proceed();

        // 记录出参
        System.out.println("Method " + methodName + " 响应参数: " + JSONUtil.toJsonStr(result));

        return result;
    }
}